using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Common;

namespace IGlobalist.Cms.Editors
{
    public partial class GetLinkList : System.Web.UI.Page
    {
        private string elements = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateScriptLinkList();
            }
        }

        private void CreateScriptLinkList()
        {
            IPage root = PageController.GetPageTreeAll(0);
            PopulateBindScript(root, "");




            string script = @"
                        var tinyMCELinkList = new Array(
	                        {0}
                        );";


            script = String.Format(script, elements);

            Response.Clear();
            Response.Write(script);
            Response.Flush();
        }



        private string GetItemLink(IPage page, string prefix)
        {
            string url = String.Format("../{0}_{1}_{2}/default.aspx", page.ID, 0, 0);

            string item = String.Format("['{0}[<i>Page</i>]{1}','{2}']", prefix, DataUtils.ScriptEscape(page.Name), url);
            return item;
        }

        private string GetItemLink(IContent content, long pageID, string prefix)
        {
            string url = String.Format("../{0}_{1}_{2}/default.aspx", pageID, 0, content.ID);
            string item = String.Format("['{0}[<i>Content</i>]{1}','{2}']", prefix, DataUtils.ScriptEscape(content.Name), url);
            return item;
        }

        private string GetItemLink(IContentCategory category, long pageID, string prefix)
        {
            string url = String.Format("../{0}_{1}_{2}/default.aspx", pageID, category.ID, 0);
            string item = String.Format("['{0}[<i>Category</i>]{1}','{2}']", prefix, DataUtils.ScriptEscape(category.Name), url);
            return item;
        }


        private void PopulateBindScript(IPage page, string prefix)
        {
            if (page.SubPageList != null)
            {
                if (page.ID > 0)
                {
                    elements = elements + ((String.IsNullOrEmpty(elements)) ? "" : ",") + GetItemLink(page, prefix);

                    foreach (IContentCategory cat in page.CategoryList)
                    {
                        elements = elements + ((String.IsNullOrEmpty(elements)) ? "" : ",") + GetItemLink(cat, page.ID, prefix + "---");
                    }

                    foreach (IContent content in page.ContentList)
                    {
                        elements = elements + ((String.IsNullOrEmpty(elements)) ? "" : ",") + GetItemLink(content, page.ID, prefix + "---");
                    }
                    prefix += "---";
                }
                foreach (IPage p in page.SubPageList)
                {

                    PopulateBindScript(p, prefix);

                }

            }
        }
    }

}
